package com.mc.moring.widge;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.mc.moring.R;
import com.mc.moring.base.HttpCallback;
import com.mc.moring.bean.WebLinkBean;
import com.mc.moring.main.WebViewActivity;
import com.mc.moring.main.modle.PermisosModle;
import com.mc.moring.util.GsonUtils;

import java.util.HashMap;
import java.util.Map;

public class MarkDialog extends Dialog {

    private JDialogCallback callback;

    public void setCallback(JDialogCallback callback) {
        this.callback = callback;
    }

    public MarkDialog(Context context) {
        super(context, R.style.JDialog_Them);
    }

    ImageView iv1;
    ImageView iv2;
    ImageView iv3;
    ImageView iv4;
    ImageView iv5;

    TextView btn_confirm;
    TextView tv_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_mark);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }

    public void initView() {
        ImageView iv_close = findViewById(R.id.iv_close);

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        iv1 = findViewById(R.id.iv1);
        iv2 = findViewById(R.id.iv2);
        iv3 = findViewById(R.id.iv3);
        iv4 = findViewById(R.id.iv4);
        iv5 = findViewById(R.id.iv5);
        tv_content = findViewById(R.id.tv_content);
        btn_confirm = findViewById(R.id.btn_confirm);

        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showIndex(1);
            }
        });

        iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showIndex(2);
            }
        });

        iv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showIndex(3);
            }
        });

        iv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showIndex(4);
            }
        });

        iv5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showIndex(5);
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (showIndex >= 4){
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+getContext().getPackageName()));
                    getContext().startActivity(intent);
                    dismiss();
                }else {
                    Map<String, String> params = new HashMap<String, String>();
                    PermisosModle.Companion.getInstant().getZiLiPageWebLinkConfig(params, new HttpCallback() {
                        @Override
                        public void onSuccess(@NonNull String data) {
                            WebLinkBean webLinkBean = GsonUtils.INSTANCE.getGson().fromJson(data, WebLinkBean.class);
                            if (webLinkBean != null && webLinkBean.getData() != null) {
                                Intent intent = new Intent(getContext(), WebViewActivity.class);
                                String url = webLinkBean.getData().getFeedbackUrl();
                                intent.putExtra("url", url);
                                getContext().startActivity(intent);
                                MarkDialog.this.dismiss();
                            }
                        }

                        @Override
                        public void onFaill(@NonNull String e) {

                        }
                    });
                }
            }
        });
    }

    private int showIndex = 0;
    private void showIndex(int index){
        showIndex = index;
        iv1.setSelected(false);
        iv2.setSelected(false);
        iv3.setSelected(false);
        iv4.setSelected(false);
        iv5.setSelected(false);
        switch (index){
            case 1:
                iv1.setSelected(true);
                break;
            case 2:
                iv1.setSelected(true);
                iv2.setSelected(true);
                break;
            case 3:
                iv1.setSelected(true);
                iv2.setSelected(true);
                iv3.setSelected(true);
                break;
            case 4:
                iv1.setSelected(true);
                iv2.setSelected(true);
                iv3.setSelected(true);
                iv4.setSelected(true);
                break;
            case 5:
                iv1.setSelected(true);
                iv2.setSelected(true);
                iv3.setSelected(true);
                iv4.setSelected(true);
                iv5.setSelected(true);
                break;
        }

        btn_confirm.setVisibility(View.VISIBLE);
        if (index >= 4){
            btn_confirm.setText("Elogiar");
            tv_content.setText("Llegó tu préstamo~ Califica nuestro \nservicio, tu apoyo nos hace seguir\n mejorando.");
        }else{
            btn_confirm.setText("Quejarse");
            tv_content.setText("Llegó tu préstamo~ Califica nuestro \nservicio, tu apoyo nos hace seguir\n mejorando.");
        }
    }

    public void initData() {

    }

    public void initEvent() {

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }
}

